<?php
    class ExpireModel extends Model {
        public function display(){
            $query = "SELECT * FROM expiredates" ;
            return $this->executeQueryAsObject($query);
        }
        public function add( $data = array() ){
            date_default_timezone_set("Asia/Bangkok");
            $fields = "";
            foreach($data as $table_name => $all_fields){
                $query = "INSERT INTO " . $table_name . " SET ";
                foreach($all_fields as $field_name => $value){
                    $fields .= "`" . $field_name . "` = '" . $this->sqlRealEscape($value) . "',";
                    //print_r($table_name);
                }
                $query .= $fields . " `created` = '" . date('Y-m-d') ."' " ;//die($query);
                return $this->executeQuery($query);
            }
        }
        public function edit( $data = array() ){
            date_default_timezone_set("Asia/Bangkok");
            $fields = "";
            foreach($data as $table_name => $all_fields){
                $query = "UPDATE " . $table_name . " SET ";
                foreach($all_fields as $field_name => $value){
                    $fields .= "`" . $field_name . "` = '" . $this->sqlRealEscape($value) . "',";
                    //print_r($table_name);
                }
                $query .= $fields . " `created` = '" . date('Y-m-d') ."' " . " WHERE `id` = '" . $data[$table_name]['id'] . "' ;" ;
                return $this->executeQuery($query);
            }
        }
        public function delete( $id = 0 ){
            $id = is_array($id)&&!empty($id)?implode(",",$id):$id;
            $this->executeQuery( "DELETE FROM expiredates WHERE id IN (" . $id . ")");
        }
        
        public function find($id){
            $query = "SELECT * FROM expiredates WHERE id ='$id' ;" ;
            return $this->executeQueryAsObject($query);
        }
    }
